Ext.define('component.telRecord.analyzePanel', {
	extend : 'Ext.form.Panel',
	initComponent:function(){
		//电话来源
		var terminalModelStoreList = [
            {"value":"0", "text":"客户"}
        ];
	    var terminalModelStore = Ext.data.Store({
	    	autoDestroy: true,
	        fields :["value","text"],
	        data : terminalModelStoreList
	    });
	    
	    //时间段
		var timeRangeStoreList = [
            {"value":"1", "text":"自定义"},
            {"value":"2", "text":"全部"}
        ];
	    var timeRangeStore = Ext.data.Store({
	    	autoDestroy: true,
	        fields :["value","text"],
	        data : timeRangeStoreList
	    });
	    
	    Ext.define('department', {
	        extend: 'Ext.data.Model',
	        fields: [
	            {name: 'id', type: 'string'},
	            {name: 'departmentCode', type: 'string'},
	            {name: 'permissionCode', type: 'string'},
	            {name: 'departmentName',  type: 'string'},
	        ]
	    });
	    
	    var departmentStore = Ext.create("Ext.data.Store", {
	    	model: 'department',
	        proxy: {
	            type: 'ajax',
	            url: jypath+'/system/user/findAllDept',
	            reader: {
	                type: 'json',
	                root: 'obj'
	            }
	        },
	        autoLoad: true
	    });
	    
	    var userStore = new Ext.data.Store({
	        fields: ["userName", "id"],
	        proxy:{
	            type:'ajax',
	            url:jypath+'/system/user/findUserByDeptPath',
	            reader:{
	                type: 'json', 
	                root: 'root'  //数据
	           }
	        },
    autoLoad: false
	    });
		var formsn = Ext.create("Ext.form.Panel", {
			minHeight:120,
		    title: "查询条件",
		    collapsible: true,  //可折叠
		    autoScroll: false,   //自动创建滚动条
		    defaultType: 'textfield',
		    fieldDefaults: {
		        margin: 2,
		        labelWidth: 80,
		        labelAlign: "right",
		        margin: "5 0 0 0"
		    },
		    items: [
		    	//权限资源隐藏字段
				{xtype:"textfield", name:"resourcesCode", value: resourcesCode, hidden: true},
				{xtype:"textfield", name:"resourcesName", value: "resName_area_list", hidden: true},
				{
					xtype : "container",
					layout : "hbox",
					items : [{
						xtype : 'combobox',
						fieldLabel : '来源',
						store:terminalModelStore,
				    	displayField: "text",
				    	valueField:"value",
				    	id:'terminalModel',
				    	name:'terminalModel',
				    	listeners:{
			                afterRender : function() {
			                	Ext.getCmp('terminalModel').setValue('0');
				            }
			            }
					},{
						xtype : 'combobox',
						fieldLabel : '员工',
						store:departmentStore,
				    	displayField: "departmentName",
				    	valueField:"permissionCode",
				    	id:'departmentCode',
				    	name:'departmentCode',
				    	width: 200,
				    	queryMode : 'local',
				    	listeners:{
			            	change:function(value){
			            		Ext.getCmp('createUserId').setValue("");
			            		userStore.load({
			            			params:{
			            				'deptPath': value.lastValue
			            			}
				            	});
			            	}
			            }
					},{
						xtype: "displayfield", value: "&nbsp;"
					},{
						xtype : 'combobox',
						store:userStore,
				    	displayField: "userName",
				    	valueField:"id",
				    	id:'createUserId',
				    	name:'createUserId',
				    	width: 120,
				    	queryMode : 'local'
					},{
						xtype : 'combobox',
						fieldLabel : '时间段',
						store:timeRangeStore,
				    	displayField: "text",
				    	valueField:"value",
				    	id:'timeRange',
				    	name:'timeRange',
				    	width: 160,
				    	listeners:{
			                afterRender: function() {
			                	Ext.getCmp('timeRange').setValue('1');
				            },
				            change: function(value){
				            	if(value.lastValue == "2"){
				            		Ext.getCmp("startTimeBeforeStr").setValue("");
					            	Ext.getCmp("startTimeAfterStr").setValue("");
				            	}
				            }
			            }
					},{
						xtype: "displayfield", value: "&nbsp;"
					},{
						xtype: "datefield", id: "startTimeBeforeStr", width: 100, name: "startTimeBeforeStr", format:'Y-m-d'
					},{
						xtype: "displayfield", value: "-"
					},{
						xtype: "datefield", id: "startTimeAfterStr", width: 100, name: "startTimeAfterStr", format:'Y-m-d'
                	}]
				}
            ],
		    buttons: [
  	        	{ xtype: "button", text: "查询", 
	  	        	handler:function(){
	  	        		gridStoresn.load();
	  	        		var pagingTool=Ext.getCmp("pagingTool");
	  	        		pagingTool.moveFirst();//跳至第一页
  	        		}  
  	        	},
	  	        { xtype: "button", text: "清空",
	  	        	handler:function(){
	  	        		formsn.getForm().reset();
	  	        	}
	  	        }
        	]    
		});
	
		var gridStoresn = Ext.create('Ext.data.Store', {
	        fields: ["createUserId","userName","departmentName", "totalCall"
	                 , "totalOutCall","totalOnOutCall","totalOffOutCall"
	                 ,"callInTen","callLtSixty","callGtSixty","avgTalkTime","totalTalkTime"],
	        pageSize: 10,  //页容量5条数据
	        //是否在服务端排序 （true的话，在客户端就不能排序）
	        remoteSort: false,
	        proxy: {
	            type: 'ajax',
	            url: jypath+'/system/telRecordAnalyze/summary_list',
	            actionMethods: { read: 'POST' },
	            reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
	                type: 'json', //返回数据类型为json格式
	                root: 'rows',  //数据
	                totalProperty: 'total' //数据总条数
	            }
	        },
	        autoLoad: true  //即时加载数据
	    });
	
		gridStoresn.on("beforeload",function(){
	        Ext.apply(gridStoresn.proxy.extraParams,formsn.getForm().getValues());
	    });
	
	//	function onreload(){
	//		gridStoresn.on("beforeload",function(){
	//	        Ext.apply(gridStoresn.proxy.extraParams,formsn.getForm().getValues());
	//	    });
	//	}
	
		var gridsn = Ext.create('Ext.grid.Panel', {
			minHeight:600,
	        store: gridStoresn,
	        columnLines: true,
	        columns: [{
	            text: '部门',
	            dataIndex: 'departmentName'
	        },{
	            text     : '员工', 
	            dataIndex: 'userName'
	        },{
	            text     : '总数',
	            dataIndex: 'totalCall'
	        },{
	            text     : '去电数',
	            dataIndex: 'totalOutCall'
	        },{
	            text     : '去电接通数',
	            dataIndex: 'totalOnOutCall'
	        },{
	            text     : '去电未接通数',
	            dataIndex: 'totalOffOutCall'
	        },{
	            text     : '10s以内',
	            dataIndex: 'callInTen'
	        },{
	            text     : '10~60s',
	            dataIndex: 'callLtSixty'
	        },{
	            text     : '60s以上',
	            dataIndex: 'callGtSixty'
	        },{
	            text     : '平均通话时长',
	            dataIndex: 'avgTalkTime'
	        },{
	            text     : '总通话时长',
	            dataIndex: 'totalTalkTime'
	        }],
	        title: '通化情况列表',
	        dockedItems: [{  
	            xtype: 'pagingtoolbar',  
	            store: gridStoresn,   // GridPanel中使用的数据  
	            dock: 'bottom',  
	            displayInfo: true,  
	            emptyMsg:"没有数据",  
	            displayMsg:"显示从{0}条数据到{1}条数据，共{2}条数据",
	            id:'pagingTool'
	        }]
	    });
	
		
		var defaultConfigg = {
			defaults: {  
	            split: true,    //有分割线  
	            collapsible: true  //可以折叠
	        },
			items:[formsn,gridsn]
		};
		Ext.apply(this,defaultConfigg);
		this.callParent(arguments);
	}
});
